#!/bin/bash 

SHELL=/bin/bash
PATH=/usr/bin:/bin:/usr/local/bin

#           h_track.sh
#  Thu Feb 18 13:18:39 2010
#  Copyright  2010  mk67
#  <rmk677@gmail.com>

## Script colours Vars.
t='\e[0;39m'	#default terminal colour
r='\e[1;31m'	#red
v='\e[1;32m'	#green
y='\e[1;36m'	#cyan
j='\e[1;33m'	#yellow
cd /home/$USER
# indispensable
temp="/home/$USER"
#TR1="activite_en_cours.html"
TR1=".ACTIVE.1"
#TR1="$temp/Dev/svn/h_tracker/activite_en_cours.html"
#TR2="activite_en_cours.html.1"
TR2=".ACTIVE.2"
#tempfile
tempf="$temp/.tmp1"
#TR2="$temp/Dev/svn/h_tracker/activite_en_cours.html.1"
DIR="$temp/Documents/Cartes/CLIM"
mkdir -p $DIR
# traduction
storm="CYCLONE"
none="CURRENTLY NO ALERT"
pre="$storm PRESENCE WARNING"
vigi="$storm VIGILANCE, CLASS 3 ALERT"
orange="$storm ORANGE ALERT, CLASS 4 ALERT"
red="$storm RED ALERT, CLASS 5 ALERT"
report="REPORT"
time="HOUR"


#variable
	query0='span.*BULLETIN'
	query1='PRESSION'
	query2='POSITION&nbsp;LE'
	query3='DISTANCE'
	query4='DEPLACEMENT'
	query5='/trkseg'
	query6='div.*c2'
	query7='span class.*s3.*font'
	query8='Il.*pas'
	query9='IL.*PAS'
	query10='NATURE.*SYSTEME'

## MAIN FUNCTION SUBFUNCTIONS
tracker(){
	if [ -f $temp/$TR1 ] ; then
		ACTIVE=$TR2
	else
		ACTIVE=$TR1
	fi
	wget -q http://www.meteo.fr/temps/domtom/La_Reunion/TGPR/actif/activite_en_cours.html -O $temp/$ACTIVE
}
nm(){
 cat -n $temp/$TR1 | grep "$query0" | awk '{ print $1 }' 
}
remove(){
if [ -f $temp/$TR1 ] ; then
	if [ -f $temp/$TR2 ] ; then
		TR1_INDATE="$( cat $temp/$TR1 | grep "$query0" | tr '&nbsp;' '/' | cut -d '/' -f 18,36 | sed -n 's/\///g;s/H//p' )"
		TR2_INDATE="$( cat $temp/$TR2 | grep "$query0" | tr '&nbsp;' '/' | cut -d '/' -f 18,36 | sed -n 's/\///g;s/H//p' )"
		TR1_DAY="$( ls -l $temp/$TR1 | awk '{ print $6 }' | sed -n 's/-//g;p' )"
		TR2_DAY="$( ls -l $temp/$TR2 | awk '{ print $6 }' | sed -n 's/-//g;p' )"
		TR1_TIME="$( ls -l $temp/$TR1 | awk '{ print $7 }' | sed -n 's/://p' )"
		TR2_TIME="$( ls -l $temp/$TR2 | awk '{ print $7 }' | sed -n 's/://p' )"
		if [ "$TR2_INDATE" != '' ] ; then
			if [ "$TR1_DAY" == "$TR2_DAY" ] && [ "$TR2_TIME" == "$TR1_TIME" ] ; then
				echo -e "\f$v  BULLETIN IDENTIQUE AU PRÉCÉDENT.\f$t"
				rm -f $temp/$TR2
				#if [ "$arg" == "c" ] ; then
				#	cron_noactivity
				#	exit 0
				#else
					exit 0
				#fi
			else
				rm -f $temp/$TR1
				mv -f $temp/$TR2 $temp/$TR1
				echo -e " $v\f ACTIVITÉ EN COURS SUR LE BASSIN:\n"
			fi
		else
			echo -e "$v\f  AUCUNE ACTIVITÉ SUR LE BASSIN.\f$t"
			if [ "$arg" == "c" ] ; then
				cron_noactivity
				exit 0
			else
				exit 0
			fi
		fi
	else
		if [ -f $temp/$TR1 ] ; then
			if [ "$TR1_INDATE" == '' ]; then
				echo -e "$v\f  AUCUNE ACTIVITÉ EN COURS.\f$t"
				if [ "$arg" == "c" ] ; then
					cron_noactivity
					exit 0
				else
					exit 0
				fi
			fi
		fi
	fi
fi
}
## cron functions
cron_var(){
	cron_file=$( crontab -l | awk '{ print $2 }' | cut -d ',' -f 1 )
	conf_file=$( echo $CRON | awk '{ print $2 }' )
}
cron_line(){
	if [ "$argX" == "x" ] ; then
		opt="-x"
	else
		opt=""
	fi
	echo "$CRON $0 -c $opt" >> .cron$USER
	crontab .cron$USER
	rm -f .cron$USER
}
cron_noactivity(){
	CRON="01 23,11 * * *"
	if [ "$( crontab -l )" == '' ] ; then
		argX=""
		cron_line
	else
		cron_var
		if [ "$cron_file" != "$conf_file" ] ; then
			argX=""
			crontab -r ; cron_line
		fi
	fi
}
cron_activity(){
if [ "$N" == "7" ] || [ "$N" == "9" ] || [ "$N" == "4" ] ; then
	CRON="01 5,11,17,23 * * *"
	cron_var
	if [ "$cron_file" != "$conf_file" ] ; then
		crontab -r ; cron_line
	fi
fi
}
cron_alert(){
if [ "$N" == "6" ] ; then
	CRON="01 2,5,8,11,14,17,20,23 * * *"
	cron_var
	if [ "$cron_file" != "$conf_file" ] ; then
		crontab -r ; cron_line
	fi
fi
}
cron_image(){
	if [ "$( crontab -l )" != '' ] ; then
		if [ "$(crontab -l | sed -n 1p | grep -w 'i')" == '' ]; then
			crontab -l | sed -n 1p > .cron$USER
		fi
	fi
	#iCRON="15 0,3,6,9,14,18,22"
	iCRON="*/60 * * * *"
	echo "$iCRON $0 -i" >> .cron$USER
	crontab .cron$USER
	rm -f .cron$USER
}
cron_image_daily(){
	if [ "$( crontab -l )" != '' ] ; then
		if [ "$(crontab -l | sed -n 1p | grep -w 'i')" == '' ]; then
			crontab -l | sed -n 1p > .cron$USER
		fi
	fi
	iCRON="01 14,23 * * *"
	echo "$iCRON $0 -i" >> .cron$USER
	crontab .cron$USER
	rm -f .cron$USER
}
cron_season(){
	iCRON="* * * 9 *"
	echo "$iCRON $0 -i -c" >> .cron$USER
	crontab .cron$USER
	rm -f .cron$USER
}
cron_check(){
	if [ "$( crontab -l )" == '' ] ; then
		echo -e "\n$v  Pas de contrôle journalier en cours.\f $t"
	else
		#if [ "$argX" == '' ] ; then
		if [ "$( crontab -l | sed -n 1p | awk '{ print $8 }' )" != '' ] ; then
			fond="."
		else
			fond="\n  en tâche de fond."
		fi
		if [ "$( crontab -l | sed -n 2p )" != '' ] ; then
			count=$( crontab -l | awk '{ print $2 }' | sed -n 1p | sed -n 's/,/h00, /g;s/$/h00/p' )
			class_type="des bulletins"
			affect="s'effectue à"
			if [ "$(crontab -l | grep -w 'i' | awk '{print $2}')" == "*" ] ; then 
				tranche="toutes les heures"
			else
				tranche="deux fois par jours"
			fi
			icount="\n$v  Relevé des images satellites $tranche."
		else
			if [ "$(crontab -l | grep -w 'i')" != '' ] ; then
			if [ "$(crontab -l | awk '{ print $2 }' | sed -n 1p)" == "*" ] ; then
				count=$( crontab -l | awk '{ print $1 }' | sed -n 1p | sed -n 's/*\///g;s/$/mn/p' )
				affect="s'effectue toutes les"
			else
				count=$( crontab -l | awk '{ print $2 }' | sed -n 1p | sed -n 's/,/h00, /g;s/$/h00/p' )
				affect="s'effectue à"
			fi
			class_type="des images satellites"
			activity_info=" Pas de contrôle d'activité en cours."
			fi
		fi
		echo -e "\n$v  Le relevé $class_type $affect $count$v $fond$icount$activity_info\n $t"
	fi
}
# satellite image donwloader
image(){
	image_view(){
	if [ "$argI" == "true" ] ; then
		if [ "$(ls -l $DIR/$current_path)" != '' ] ; then
			eog $sat_path/$(ls -ltr $sat_path | sed -n '$p' | awk '{print $9}')
		fi
	fi
	}
	actual=$( date +%Y%m%d%H%M)
	if [ -s $tempf ] ; then
		#previous=$( cat $tempf | sed -n "2s/^/"$(ls -l .tmp1 | awk '{print $6}'|cut -d '-' -f 2)"/;s/0//p" )
		#previous=$( cat $tempf | sed -n "2s/^/"$(ls -l .tmp1 | awk '{print $6}'|cut -d '-' -f 1,2)"/p" )
		previous=$( cat $tempf | sed -n "2s/^/"$(ls -l .tmp1 | awk '{print $6}' | sed -n 's/-//g;s/\([0-9]\{2\}\)$//;p')"/p" )
	else
		previous=0
	fi
	if [ "$[$actual-$previous]" -ge "10000" ] ; then
		current_path="daily_sat"
		mkdir -p $DIR/$current_path
		sat_path=$(date +%y-%m)
	else
		if [ "$( cat $tempf | grep -w 'EX' )" == '' ] ; then
			current_path=$(cat $tempf | sed -n 1p)
			sat_path="sat"
		else
			current_path=$(cat $tempf | sed -n 1p | cut -d '-' -f 2)
			sat_path="sat"
		fi
	fi
	chmod 755 $DIR/$current_path
	cd $DIR/$current_path
	if [ "$(ls -d $DIR/$current_path/$sat_path 2>/dev/null)" == '' ] ; then
		mkdir -p $sat_path ; chmod 755 $sat_path
	fi
	image_view
	#ls -l $DIR/$current_path/sat/ ; 
	#ls -l sat/
	#if [ "$(crontab -l)" != '' ] ; then
	#	if [ "$( crontab -l | grep 'i' )" != '' ] ; then
	#		opt_cmd=9
	#	fi
	#else
	#	opt_cmd=8
	#fi
	#if [ "$(ls $sat_path )" == '' ] ; then
	#	last_jpeg=0
	#else
	#	last_jpeg=$(ls -ltr $sat_path | sed -n '$p' | awk '{print $('$opt_cmd')}' | cut -d '_' -f 2 | cut -d '.' -f 1)
	#fi
	#wget -q http://www.meteo.fr/temps/domtom/La_Reunion/TGPR/saison/images/satellite/DerImageSatellite.jpg -O $sat_path/sat_$(echo $[$last_jpeg+1]).jpg
	wget -q http://www.meteo.fr/temps/domtom/La_Reunion/TGPR/saison/images/satellite/DerImageSatellite.jpg -O $sat_path/$(date +%d-%H%M).jpg
	cd $temp
}
archive_file(){
	if [[ "$( date +%b )" == "april"||"avril" ]] ; then
		mkdir -p $DIR/archive
		cd $DIR
		DIR_T=$(echo $DIR | sed -n 's/\//\\\//g;s/$/\\\//p')
		#echo $DIR | tr '/' '-' | sed -n 's/-/\\\//g;s/$/\\\//p'
		TAR_LIST=$(ls -ld $DIR/[[:upper:]]* | sed -n "s/"$(echo $DIR_T)"//p" | awk '{ print $8 }')
		if [ "$TAR_LIST" != '' ] ; then
			for tar_file in $TAR_LIST ; do
				tar acf ./archive/$tar_file.tar.gz $tar_file
				cp -f ./archive/$tar_file.tar.gz ./archive/$(date +%Y)
				#rm -r $DIR/$tar_file 
			done
		fi
	fi
}
archive_season(){
	if [[ "$( date +%b )" == "may"||"may" ]] ; then
		mkdir -p $DIR/$(date +Y%)
		archive_file
		cron_season
	fi
}
#x
##MAIN FUNCTION
TRACK(){
# creation de base du ficheir gpx
point_start(){
	echo -e "<?xml version='1.0' encoding='UTF-8'?>" >> $gpx_file
	echo -e "<gpx version=\"1.1\" creator=\"h_tracker\" xmlns=\"$(uname -n) $(uname -o)\"" >> $gpx_file
	echo -e "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" >> $gpx_file
	echo -e "    xsi:schemaLocation=\"$(uname -n) $(uname -o) $DIR/${CURRENT%<}\">\n  <metadata>" >> $gpx_file
	echo -e "    <bounds minlat=\"-$lat\" minlon=\"$lon\" maxlat=\"-$lat\" maxlon=\"$lon\" />\n  </metadata>" >> $gpx_file
	echo -e "  <trk>\n    <trkseg>" >> $gpx_file
	echo -e "      <trkpt lat=\"-$lat\" lon=\"$lon\">\n        <time>$( date +%Y-%m-%dT\ %H:%m:00Z )</time>\n      </trkpt>\n" >> $gpx_file
	echo -e "    </trkseg>\n  </trk>\n</gpx>" >> $gpx_file
}
# nouveau point gpx
point_new(){
	MAX=$( cat $gpx_file | grep "maxlat" | cut -d ' ' -f 8,9 )
	OLD=$( sed -n "s/`echo $MAX`/maxlat=\"-`echo $lat`\" maxlon=\"`echo $lon`\"/p" $gpx_file )
	sed -i "6c \    `echo $OLD`" $gpx_file
	ngpx=$(cat -n $gpx_file | grep -w "$query5" | awk '{ print $1 }')  #> .tmp2
	#sed -i "$[$(cat .tmp2)-1]c\      <trkpt lat=\"-$lat\" lon=\"$lon\">\n        <time>$( date +%Y-%m-%dT\ %H:%m:00Z )</time>\n      </trkpt>\n" $gpx_file 
	sed -i "$(echo $[$ngpx-1])c\      <trkpt lat=\"-$lat\" lon=\"$lon\">\n        <time>$( date +%Y-%m-%dT\ %H:%m:00Z )</time>\n      </trkpt>\n" $gpx_file 
}
# genrate temp file
new_tempf(){
	echo -e "$( echo ${txt1%<} | awk '{ print $'$PN' }') \n$( echo $txt0 | awk '{ print $3$6 }' | sed -n 's/H//p' )" > $tempf
}
# migration sur changement de dénomination ou nom
migrate(){
if [ -f $tempf ] && [ -f $gpx_file ] ; then
	if [ "$( sed -n '1p' $tempf)" != "$( echo ${txt1%<} | awk '{ print $4 }' )" ] ; then
		if [ "$( sed -n '2p' $tempf)" != "$( echo $txt0 | awk '{ print $3$6 }' | sed -n 's/H//p' )" ] ; then
			if [[ "$(( $( echo $txt0 | awk '{ print $3$6 }' | sed -n 's/H//p' ) - $( sed -n '2p' $tempf) ))" == 600||300 ]] ; then
				if [[ "$( echo ${txt1%<} | awk '{ print $4 }' | cut -d '-' -f 1 )" == 'EX' ]] ; then
					OLDG=$( echo ${txt1%<} | awk '{ print $4 }' )
					NEWG=$( sed -n '1p' $tempf)
				else
					OLDG=$( sed -n '1p' $tempf)
					NEWG=$( echo ${txt1%<} | awk '{ print $4 }' )
				fi
				sed -n '/<trkpt/,/\/gpx>/p' $DIR/$OLDG/$OLDG.gpx >> $DIR/$NEWG/$NEWG.gpx # ancien vers nouveau
				sed -n '10,16d;p' -i $DIR/$NEWG/$NEWG.gpx # supprime les donnée inutile
				cp -f $DIR/$OLDG/*.txt $DIR/$NEWG
				point_new
				cd $DIR
				rm -f -R $OLD
				cd /home/$USER
			fi
		fi
	fi
fi
new_tempf
}
# download
	tracker
# compare old file with new and remove old when necessary
	remove
# bulletin date
	txt0=$( cat $temp/$TR1 | grep "$query0" | tr '&nbsp;' ':' | cut -d ':' -f 6,12,18,24,30,36 | tr ':' ' ')
#nature
n=$(nm)
if [ "$( cat $temp/$TR1 | grep -w "$query7" | sed -n 's/\;/:/g;s/ /:/g;s/&/:/g;p' | cut -d ':' -f 20 )" == "pas" ] ; then
	N=7 #; echo -e "$y$N"
fi
if [ $( cat $temp/$TR1 | sed -n "`echo $(( $n+2 ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 1 | tr ':' ' ' ) == "IL" ] ; then
	if [ $( cat $temp/$TR1 | sed -n "`echo $(( $n+9 ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 1 | tr ':' ' ' ) == "PRESSION" ] ; then
		N=7 #; echo -e "$y$N"
	else
		N=9 #; echo -e "$y$N"
	fi
else
	if [ $( cat $temp/$TR1 | sed -n "`echo $(( $n+4 ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 1 | tr ':' ' ' ) == "DEPRESSION" ] ; then
		N=4 #; echo -e "$y$N"
	else
		N=6 #; echo -e "$y$N"
	fi
fi
txt1=$( cat $temp/$TR1 | sed -n "`echo $(( $n+$N ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 1,7,13,19 | tr ':' ' ' )
##pression
	txt2=$( cat $temp/$TR1 | grep -w "$query1" | tr '&nbsp;' ':' | cut -d ':' -f 32,38 | tr ':' ' ' )
##position
	txt3=$( cat $temp/$TR1 | grep -w "$query2" | tr '&nbsp;' ':' | cut -d ':' -f 50,56,62,68,74,80 | tr ':' ' ' )
##distance
	txt4=$( cat $temp/$TR1 | grep -w "$query3" | tr '&nbsp;' ':' | cut -d ':' -f 44,50,56,62 | tr ':' ' ' )
##deplacement
	txt5=$( cat $temp/$TR1 | grep -w "$query4" | tr '&nbsp;' ':' | cut -d ':' -f 152,158,164,170 | tr ':' ' ' )
##type alerte
#if [ $( cat $temp/$TR1 | sed -n "`echo $(( $n+2 ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 1 | tr ':' ' ' ) == "IL" ] ; then
if [ "$N" == "7" ] || [ "$N" == "9" ] || [ "$N" == "4" ] ; then
	alert="PAS D'ALERTE EN COURS"
	if [ "$arg" == "c" ] ; then
		cron_activity
	fi
else
	alert=$( cat $temp/$TR1 | sed -n "$(echo $[ $n+2 ])"p | tr '&nbsp;' ':' | cut -d ':' -f 1,7 | tr ':' ' ' )
	if [ "$arg" == "c" ] ; then
		cron_alert
	fi
fi
##display
echo -e "\f$v  ${txt0#*>} \n  ${txt1%<} \n  $j${txt2%.<} \n ${txt3%<} \n  ${txt4%<} \n  ${txt5%.<} \n\f  $v$alert \n $t"
#; echo -e "$y$N" ; echo -e "$y$n"
## current storm folder creation
if [ "$( echo $txt1 | awk '{ print $3 }' )" == "$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 13 )" ] ; then
	#echo -e "$y 13"
	#echo -e "$j$( cat $temp/$TR1 | sed -n "`echo $(( $n+$N ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 13 )"
	if [ "$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 13 )" != '' ] ; then
		PN=3
		#if [ $( cat $temp/$TR1 | sed -n "`echo $(( $n+$N ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 13 | cut -d '-' -f 1 ) == "EX" ] ; then
		if [ "$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 13 | cut -d '-' -f 1 )" == "EX" ] ; then
			CURRENT=$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 13 | cut -d '-' -f 2 )
		else
			CURRENT=$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 13 )
		fi
	fi
fi
if [ "$( echo $txt1 | awk '{ print $4 }' )" == "$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 )" ] ; then
	#echo -e "$y 19"
	#echo -e "$r$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 )"
	if [ "$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 )" != '' ] ; then
		PN=4
		#if [[ "$( cat $temp/$TR1 | sed -n "`echo $(( $n+$N ))`"p | tr '&nbsp;' ':' | cut -d ':' -f 19 | cut -d '-' -f 1 )" == "EX" ]] ; then
		if [[ "$( cat $temp/$TR1 | sed -n "$(echo $[$n+$N])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 | cut -d '-' -f 1 )" == "EX" ]] ; then
			CURRENT=$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 | cut -d '-' -f 2 )
		else
			CURRENT=$( cat $temp/$TR1 | sed -n "$(echo $[ $n+$N ])"p | tr '&nbsp;' ':' | cut -d ':' -f 19 )
		fi
	fi
fi
mkdir -p $DIR/${CURRENT%<} #; echo -e "$j$CURRENT ** "
## current storm new point file creation & display
echo -e "${txt0#*>} \n ${txt1%<} \n ${txt2%.<} \n${txt3%<} \n ${txt4%<} \n ${txt5%.<}" > $DIR/${CURRENT%<}/$( date +%y%m%d%H ).txt
txt_file="$DIR/${CURRENT%<}/$( date +%y%m%d%H ).txt"
gpx_file="$DIR/${CURRENT%<}/${CURRENT%<}.gpx"
## gpx file : tested with kde4 marble, probably others
lon=$( cat $txt_file | awk '{print $4}'| sed -n 4p )
lat=$( cat $txt_file | awk '{print $1}'| sed -n 4p )
if [ "$( ls $gpx_file 2>/dev/null )" == '' ] ; then
	point_start
	migrate
else
	#if [ "$( cat $tempf | sed -n 1p )" == "$( echo ${CURRENT%<} )" ] ; then
		new_tempf
		point_new
	#fi
fi
}
## MAIN OPTIONS
while [ "$#" -gt 0 ] ; do
	if [ "$#" -ge 1 ] ; then
		if [ "$1" == '-k' ] || [ "$2" == '-k' ]; then
		ht=6 ; lg=60
		elif [ "$1" == '-c' ] ; then
		display=" -display :0"
		else
		ht=16 ; lg=40
		fi
		if [ "$1" == "-x" ]; then
			if [ "$2" == '' ] ; then
				echo -e "\n$y L'option$j -x$y ne peut pas s'exécuter seule.$t\n" ; exit 1
			else
				echo -e "\n$y Placer l'option$j -x$y en seconde position.$t\n" ; exit 1
			fi
		fi
	fi
	termx='xterm'$display' -hold -sb -bg black -bd black -bw 0 -geometry '$lg'x'$ht' -e '$0''
	case "$1" in
		-k|--check)
				if [ "$#" -ge "2" ] ; then
					if [ "$1" != "-x" ] | [ "$2" != "-x" ]; then
						$0 CC ; shift 1
					else
						$termx CCX ; shift 2
					fi
				else
					$0 CC ; exit 0
				fi
		;;
		-p|--pontually)
				if [ "$#" -ge "2" ] ; then
					if [ "$1" != "-x" ] | [ "$2" != "-x" ]; then
						$0 T ; shift 1
					else
						$termx TX ; shift 2
					fi
				else
					$0 T ; exit 0
				fi
		;;
		-c|--cron)
			rm -f .cron$USER
				if [ "$#" == "2" ] ; then
					if [ "$1" != "-x" ] | [ "$2" != "-x" ]; then
						$0 C ; shift 1
					else
						$termx CX ; shift 2
					fi
				else
					$0 C ; exit 0
				fi
		;;
		-s|--stop)
			crontab -r ; rm -f .cron$USER ; exit 0
		;;
		-r|--reset)
			rm -f $temp/$TR1 ; rm -f $temp/$TR2 ; exit 0
		;;
		-i|--image)
			if [ "$#" -ge "2" ] ; then
					if [ "$1" != "-x" ] | [ "$2" != "-x" ]; then
						$0 I ; shift 1
					else
						$0 I ; shift 2
					fi
				else
					$0 I ; exit 0
			fi
		;;
		-hi|--hourly-image)
			$0 IC ; exit 0
		;;
		-di|--daily-image)
			$0 ID ; exit 0
		;;
		-iw|--image-wiever)
			if [ "$#" -ge "2" ] ; then
					if [ "$1" != "-x" ] | [ "$2" != "-x" ]; then
						$0 IW ; shift 1
					else
						$0 IW ; shift 2
					fi
				else
					$0 IW ; exit 0
			fi
		;;
		''|-h|--help)
			echo -e "\nusage : $(basename $0) (opt1) (opt2) (opt3)"
			echo -e "\n L'option$j x$t doit être placée obligatoirement après$j -p, -k, -c$t\n dont elle dépends."
			echo -e "\t -p, --ponctually \t one shot without cron check."
			echo -e "\t -c, --cronly \t\t with cron automate support."
			echo -e "\t -s, --stop \t\t stop script automation \n\t\t\t\t and remove all crontab user file. "
			echo -e "\t -k, --check \t\t check cron config."
			echo -e "\t -r, --reset \t\t reset html file ( in case of dl mistake)."
			echo -e "\t -i, --image \t\t dowload ponctually satellite image"
			echo -e "\t -di, --daily-image \t check daily satellite image"
			echo -e "\t -hi, --hourly-image \t check hourly satellite image."
			if [ -x /usr/bin/eog ] ; then
				echo -e "\t -iw, --image-viewer \t display satellite image "
			fi
			echo -e "\t -x, --xterm \t\t xterm pseudo graphic ui."
			echo -e "\n\t -h, --help \t\t this help."
			echo -e "\n\t -e, --exemple \t\t commande line exemples."
			shift
		;;
		-e)
			echo -e "\n$r Important$t:\n Le répertoire par default est$j $DIR$t"
			echo -e " Modifiez la ligne$j 28$t du script pour diriger les données vers un autre dossier."
			echo -e "\n$r Rappel$t:\n L'option$j x$t ne peut être exécutée seule et doit être placée"
			echo -e " obligatoirement après$j -p, -k, -c$t dont elle dépends."
			echo -e "\n exemples:\n\t $j$(basename $0) -p -x -i\n\t $(basename $0) -i -p -x$t"
			echo -e " exécute le script ponctuellement en pseudo GUI Xterm avec téléchargement\n de l'image satellite."
			echo -e "\n autres exemples:\n\t $j$(basename $0) -p -i\n\t $(basename $0) -i -p$t"
			echo -e " exécute le script dans le terminal ponctuellement avec téléchargement\n de l'image satellite."
			echo -e "\n\t $j$(basename $0) -c -di\n\t $(basename $0) -c -hi$t"
			echo -e " exécute le script selon les règles cron respectivement avec téléchargement"
			echo -e " de l'image satellite journalier ou toutes les heures."
			echo -e " Et ainsi de suite ... \n"
			shift #; exit 1
		;;
		L)
			echo -e "-k\n-p\n-c\n-r\n-s\n-i\n-hi\n-di\n-iw\n-h\n-x"
			shift 
		;;
		T )
			#echo -e "$y no cron"
			TRACK
			shift
		;;
		C)
			arg="c" ; rm -f .cron$USER
			#echo -e "$y cron"
			TRACK
			shift
		;;
		CC)
			#echo -e "$y check cron"
			cron_check
			shift
		;;
		TX)
			argX="x"
			#echo -e "$y no cron"
			TRACK
			shift
		;;
		CX)
			arg="c" ; argX="x" ; rm -f .cron$USER
			#echo -e "$y cron"
			TRACK
			shift
		;;
		CCX)
			argX="x" 
			#echo -e "$y check cron"
			cron_check
			shift
		;;
		I)
			image
			shift
		;;
		IC)
			cron_image
			shift
		;;
		ID)
			cron_image_daily
			shift
		;;
		IW)
			argI='true'
			image
			shift
		;;
		tf)
			archive_file ; exit 0
		;;
		*)
			echo -e "\n Option$j $@$t inconnue, faites$j $(basename $0) -h$t ou$j --help$t pour"
			echo -e " obtenir la liste ou faite$j $(basename $0) -e$t pour obtenir des exemples.\n"
			shift
		;;
	esac
done
exit 0